﻿using System.Reflection.Emit;

var printMethod = new DynamicMethod("PrintHello", null, Array.Empty<Type>());
//生成IL代码
ILGenerator iLGenerator = printMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Nop);
iLGenerator.Emit(OpCodes.Ldstr, "Hello World");
var writeLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
iLGenerator.Emit(OpCodes.Call, writeLineMethod);
iLGenerator.Emit(OpCodes.Nop);
iLGenerator.Emit(OpCodes.Ret);
//创建委托
var printAction=printMethod.CreateDelegate(typeof(Action)) as Action;
printAction?.Invoke();


